package org.javaboy.vhr.exception;

import org.javaboy.vhr.model.RespBean;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;


@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLException.class)
    public RespBean sqlException(SQLException e) {
        if (e instanceof SQLIntegrityConstraintViolationException) {
            System.out.println("异常为"+e);
            return RespBean.error("该数据有关联数据，操作失败!");//统一格式返回给前端
        }
        //此处可以多个if接到不同的异常类型分类处理
        System.out.println("sql异常为"+e+e.getMessage());
        return RespBean.error("数据库异常，操作失败!");
    }
}